import tkinter as tk
from page.SetPage import SetPage
from page.UsePage import UsePage
from page.Config import Config 
from page.LoginWindow import LoginWindow
from PIL import ImageTk, Image

# 主界面
class StartPage(tk.Frame):
    
    winWidth = 800
    winHeight = 400
    bgSrc = "resource/bg/bg.png"

    def __init__(self, parent, controller):
        config = Config()

        tk.Frame.__init__(self, parent)
        self.configure(bg="#FFA500")

        # 设置背景图片
        bg_img = ImageTk.PhotoImage(UsePage.show_image_with_size(UsePage,rf"{Config.resource_path(self.bgSrc)}"))
        # 在Frame中创建Label作为背景
        self.bg_canvas = tk.Canvas(self, width=self.winWidth, height=self.winHeight,bg="white", highlightthickness=0)
        self.bg_canvas.place(x=0, y=0, relwidth=1, relheight=1)
        self.bg_canvas.create_image(
            0, 0,
            image=bg_img,
            anchor='nw',
            tags="bg"
        )
        self.bg_canvas.bg = bg_img

        # 设置grid布局 3x4
        for row in range(3):
            self.rowconfigure(row, weight=1)
        for col in range(4):        
            self.columnconfigure(col, weight=1)
        
        btn1 = tk.Button(self, text="设置",font=('微软雅黑',30),command=lambda: self.is_login(controller))
        btn1.grid(row=1,column=1,sticky="nsew", padx=2, pady=2)

        btn2 = tk.Button(self, text="抽奖",font=('微软雅黑',30),command=lambda: self.goToUse(controller))
        btn2.grid(row=1,column=2,sticky="nsew", padx=2, pady=2)

    def is_login(self,controller):
        # 实例化
        config = Config()
        # 判读是否登录
        if(config.data['loginStatus']):
            # 已登录的直接跳转
            controller.show_frame(SetPage)
        else:
            LoginWindow.open_login_window()
            
    def goToUse(self,controller):
        # 获取屏幕尺寸
        screen_width = controller.winfo_screenwidth()
        screen_height = controller.winfo_screenheight()

        # 设置窗口大小
        controller.geometry(f"{screen_width}x{screen_height}+0+0")
        controller.show_frame(UsePage)

    # 进入页面
    def on_enter(self):
        pass
    
    # 离开页面
    def on_leave(self):
        pass